import sqlite3
import os

def migrate_database():
    """为matches表添加所有必要的列"""
    
    db_path = os.path.join('app', 'esports.db')
    
    if not os.path.exists(db_path):
        print(f"数据库文件不存在: {db_path}")
        return
    
    print(f"尝试连接数据库: {db_path}")
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    # 获取当前matches表结构
    cursor.execute("PRAGMA table_info(matches)")
    columns = cursor.fetchall()
    column_names = [column[1] for column in columns]
    
    # 需要添加的列及其默认值
    columns_to_add = {
        'game_type': 'TEXT DEFAULT "lol"',
        'profit': 'FLOAT DEFAULT 0',
        'investment': 'FLOAT DEFAULT 0',
        'return_amount': 'FLOAT DEFAULT 0',
        'match_format': 'TEXT DEFAULT "Bo3"',
        'team1_blue_side': 'BOOLEAN DEFAULT 1',
        'total_games': 'INTEGER DEFAULT 3',
        'games_json': 'TEXT DEFAULT "{}"'
    }
    
    # 检查并添加缺少的列
    for column_name, column_type in columns_to_add.items():
        if column_name not in column_names:
            print(f"添加 '{column_name}' 列到 matches 表...")
            try:
                cursor.execute(f"ALTER TABLE matches ADD COLUMN {column_name} {column_type}")
                conn.commit()
                print(f"成功添加 '{column_name}' 列")
            except sqlite3.Error as e:
                print(f"添加列 '{column_name}' 时出错: {e}")
        else:
            print(f"'{column_name}' 列已存在")
    
    conn.close()
    print("迁移完成")

if __name__ == "__main__":
    migrate_database() 